package com.znzz.course.util.db;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.znzz.course.entity.Course;
import com.znzz.course.entity.Student;

import java.util.ArrayList;

/**
 * 学生的本地数据库方法
 * @Author by DHF
 * @Date 2022/04/2022/4/22 10:38
 * @Version 1.0
 */
public class StudentDao {
    private Context context;

    public StudentDao(Context context) {
        this.context = context;
    }
    //保存学生信息
    public void saveData(Student student) {
        SQLiteDatabase sqLiteDatabase =  DBManager.getSqliteWritableDatabase(context);
        sqLiteDatabase.execSQL
                ("insert into student( studentId, name, password, sex,phone,  clazzId,major,collage) " +
                                "values( ?, ?, ?, ?, ?,?,?,?)",
                        new String[] {
                                student.getStudentId(),
                                student.getName(),
                                student.getPassword(),
                                student.getSex(),
                                student.getPhone(),
                                student.getClazzId(),
                                student.getMajor(),
                                student.getCollage()}
                );
        sqLiteDatabase.close();
    }
    //得到学生的信息
    @SuppressLint("Range")
    public Student loadData() {
        Student student = new Student();
        SQLiteDatabase sqLiteDatabase = DBManager.getSqliteWritableDatabase(context);
        Cursor cursor = sqLiteDatabase.rawQuery("select * from student", null);
        if (cursor.moveToFirst()) {
            do {
                student.setId(cursor.getInt(cursor.getColumnIndex("id")));
                student.setStudentId(cursor.getString(cursor.getColumnIndex("studentId")));
                student.setName(cursor.getString(cursor.getColumnIndex("name")));
                student.setPassword(cursor.getString(cursor.getColumnIndex("password")));
                student.setSex(cursor.getString(cursor.getColumnIndex("sex")));
                student.setMajor(cursor.getString(cursor.getColumnIndex("major")));
                student.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
                student.setCollage(cursor.getString(cursor.getColumnIndex("collage")));
                student.setClazzId(cursor.getString(cursor.getColumnIndex("clazzId")));
            } while(cursor.moveToNext());
        }
        cursor.close();
        return student;
    }
    /**
     * 删除
     */
    public void deleteStudent(){
        SQLiteDatabase sqLiteDatabase = DBManager.getSqliteWritableDatabase(context);
        sqLiteDatabase.execSQL("delete from student");
        sqLiteDatabase.close();
    }

    //修改电话
    public void updatePhone(String phone){
        SQLiteDatabase sqLiteDatabase = DBManager.getSqliteWritableDatabase(context);
        sqLiteDatabase.execSQL("update student set phone ='"+phone+"'");
        sqLiteDatabase.close();
    }
}
